<?php
/**
 * Created by : PhpStorm
 * User: Shane
 * Date: 2023-03-24
 * Time: 17:24
 */

namespace app\admin\controller;

use app\AdminBaseController;
use Symfony\Component\VarExporter\VarExporter;
use think\Exception;
use think\facade\Config;

class Sms extends AdminBaseController
{
    public function index(){
        $sms_lit = Config::get('sms');
        if($this->request->post()){
            $data = $this->request->post();
            if(isset($sms_lit[$data['type']])){
                $actions =[];
                if(isset($data['template'])){
                    foreach ($data['template']['yw'] as $k =>$v){
                        $actions[$v]=[
                            'actions_name'      => $data['template']['zw'][$k],
                            'template_id'  => $data['template']['mb'][$k],
                        ];
                    }
                }
                $sms_lit[$data['type']]['actions'] = $actions;
                foreach ($sms_lit[$data['type']]  as $k=>$v){
                    if(isset($data[$k])){
                        $sms_lit[$data['type']][$k] = $data[$k];
                    }
                }
                $file = __DIR__ . '/../../../config/sms.php';
                $ret = file_put_contents($file, "<?php\n\n" . "return " . VarExporter::export($sms_lit) . ";\n", LOCK_EX);
                if (!$ret) {
                    exit(json_encode(["code"=>400,"msg"=>"配置写入失败！"]));
                }else{
                    exit(json_encode(["code"=>200,"msg"=>"保存成功！"]));
                }
            }else{
                exit(json_encode(["code"=>400,"msg"=>"配置参数错误！"]));
            }

        }
        return view('index',['sms_lit'=>$sms_lit]);
    }
}